import os

import cv2
import matplotlib.pyplot as plt
from PIL import Image

# pip install opencv-python
'''
'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r',
 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r',
  'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 
  'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu',
   'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1',
    'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu',
     'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r',
      'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 
      'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r',
       'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 
       'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r',
        'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r',
         'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 
         'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo',
          'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'
'''

'''
Greys_r
gray
gist_yarg_r
binary_r


'''

def imggray(path, newPath):
    # open colour image
    color = 'binary_r'
    image_raw = Image.open(path)
    image_gray = image_raw.convert('L')
    plt.figure('sunflower')  # 图名
    plt.imshow(image_gray, cmap=color)  # cmap即colormap，颜色映射
    plt.axis('off')  # 关闭网格线
    plt.imsave(newPath, image_gray, cmap=color)


def imgDimming(path, newPath):
    # open colour image
    image_raw = cv2.imread(path)
    image_raw = image_raw * 0.5
    cv2.imwrite(newPath, image_raw)


def imgFils(path, type):
    # 获取文件列表
    for dirpath, dirnames, filenames in os.walk(path):
        print(dirpath)
        if dirpath.__contains__("changed_img"):
            continue
        if type == 1 and not os.path.exists(os.path.join(dirpath, "changed_img_color")):
            os.makedirs(os.path.join(dirpath, "changed_img_color"))
        if type == 2 and not os.path.exists(os.path.join(dirpath, "changed_img")):
            os.makedirs(os.path.join(dirpath, "changed_img"))
        for filename in filenames:
            try:
                if filename.__contains__(".jpg") or filename.__contains__(".png"):
                    imgpath = os.path.join(dirpath, filename)
                    if type == 1:
                        new_imgpath = os.path.join(dirpath, "changed_img_color", filename)
                        imggray(imgpath, new_imgpath)
                    if type == 2:
                        new_imgpath = os.path.join(dirpath, "changed_img", filename)
                        imgDimming(imgpath, new_imgpath)
            except Exception as e:
                print(e)
                continue


if __name__ == '__main__':
    type = 1
    imgFils(r"D:\like\img-weight", type)
